/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package server.persistencia;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import server.dominio.Hora;
import server.dominio.Medico;



/**
 *
 * @author tpaxle
 */
public class DAOHoraMySQL extends DAOHora{
    private Agente Broker;
    private Connection Con;
    private DateFormat formatear;
    public DAOHoraMySQL() throws SQLException{
            Broker= Agente.getAgente();
            Con= Broker.getConnection();
            formatear= new SimpleDateFormat("yyyy-MM-dd HH:mm");
    }
    @Override
    public ArrayList<Object> select(String condicion) throws SQLException {
        ArrayList<Object> listaHora= new ArrayList<Object>();
        Statement stmt;

        DAOMedicoMySQL daoM= new DAOMedicoMySQL();

        stmt = (Statement) Con.createStatement();
        ResultSet res  = stmt.executeQuery(condicion);

         while(res.next()) {
             java.util.Date horas = null;
            try {
                horas = formatear.parse(res.getString(3));
     
            } catch (ParseException ex) {
                Logger.getLogger(DAOHoraMySQL.class.getName()).log(Level.SEVERE, null, ex);
            }

             Medico m=(Medico) daoM.find("SELECT * FROM Medico Where idMedico_pk= "+res.getInt("id_medico_fk"));
             Hora hora= new Hora(res.getInt(1), m, horas, res.getString(4));
             listaHora.add(hora);
         }
         res.close();
         stmt.close();
         daoM.cerrarDAO();
         this.cerrarDAO();

        return listaHora;
    }

    @Override
    public int insert(Object obj) throws SQLException {
        Hora hora_aux= (Hora)obj;
        int resul=0;

      
        /*La persona no se inserta aki Debe estar ya insertada*/
        PreparedStatement stmt = (PreparedStatement) Con.prepareStatement("INSERT INTO Horario(id_Medico_fk, Fecha_trabajo, estado) VALUES ((SELECT idMedico_pk FROM " +
                "Medico, Usuario WHERE nif= ? and Usuario.idUsuario_pk= Medico.idUsuario_fk),?,?)");
        stmt.setString(1, hora_aux.getMedico().getNif());
        stmt.setString(2, formatear.format(hora_aux.getFecha_trabajo()));
        stmt.setString(3, hora_aux.getEstado());
        resul= stmt.executeUpdate();

         stmt.close();

        return resul;
    }

    @Override
    public int update(Object obj) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public int delete(Object hora) throws SQLException {
            PreparedStatement stmt;
            int result= 0;
            stmt = (PreparedStatement) Con.prepareStatement("DELETE FROM Horario WHERE id_Horario= ?");
            stmt.setInt(1, ((Hora)hora).getId_Horario());
            result= stmt.executeUpdate();

            return result;
    }

    @Override
    public Object find(String condicion) throws SQLException {
        Hora hora= null;
	Statement stmt;
        DAOMedicoMySQL daom= new DAOMedicoMySQL();
	stmt = (Statement) Con.createStatement();
	ResultSet res  = stmt.executeQuery(condicion);

        if(res.next()){
                Medico medico= (Medico) daom.find("SELECT * FROM Medico WHERE idMedico_pk= "+res.getInt("id_Medico_fk"));
                if(medico!= null){
                try {
                    hora = new Hora(res.getInt("id_Horario"), medico, res.getString("Fecha_trabajo"), res.getString("estado"));
                } catch (ParseException ex) {
                    System.out.println("Imposible tratar hora");
                }
                }

        }
        daom.cerrarDAO();
        return hora;
    }

    @Override
    public void cerrarDAO() throws SQLException {
        Con.close();
    }

}
